import redis
import time

def check_replication_delay(master_host, master_port, master_password, slave_host, slave_port, slave_password):
    # 连接到 Redis 主从服务器
    r_master = redis.Redis(host=master_host, port=master_port, password=master_password)
    r_slave = redis.Redis(host=slave_host, port=slave_port, password=slave_password)

    # 在主库写入数据并记录当前时间戳
    data = "test_data"
    start_time = time.time()
    r_master.set("test_key", data)

    # 在从库获取数据并记录当前时间戳
    while True:
        if r_slave.get("test_key") == data:
            end_time = time.time()
            break

    # 计算时间延迟
    replication_delay = end_time - start_time

    return replication_delay


delay = check_replication_delay('172.16.67.157',7712,'fjAskld.fj87623','172.16.66.168',7712,'fjAskld.fj87623')
print(f"There is a replication delay of {delay} seconds between master and slave.")